﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using BusinessObjects.BussinessVoucher;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;
using DataObjects.StandartInterfaces.Voucher;

namespace DataObjects.ADO.SqlServer.Voucher
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerItemTransactionDetailDao : IItemTransactionDetailDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="itemTransactionDetail">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(ItemTransactionDetail itemTransactionDetail)  
        {
            return new object[]  
            {               
                 "@ItemTransactionDetailID", itemTransactionDetail.ItemTransactionDetailId,
                "@InventoryItemID", itemTransactionDetail.InventoryItemId,
                "@AccountNumber", itemTransactionDetail.AccountNumber,
                "@CorrespondingAccountNumber", itemTransactionDetail.CorrespondingAccountNumber,

                  "@Description", itemTransactionDetail.Description,
                "@AmountOc", itemTransactionDetail.AmountOc,
                "@AmountVat", itemTransactionDetail.AmountVat,
                "@TaxRate", itemTransactionDetail.TaxRate,

                  "@AccountNumberVat", itemTransactionDetail.AccountNumberVat,
                "@RefID", itemTransactionDetail.RefId,
                "@Quantity", itemTransactionDetail.Quantity,
                "@Price", itemTransactionDetail.Price,
                "@VoucherTypeID", itemTransactionDetail.VoucherTypeId,
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, ItemTransactionDetail> Make = reader =>
          new ItemTransactionDetail
          {
              ItemTransactionDetailId = reader["ItemTransactionDetailID"].AsLong(),
            InventoryItemId = reader["InventoryItemId"].AsInt(),
            AccountNumber = reader["AccountNumber"].AsString(),
            CorrespondingAccountNumber = reader["CorrespondingAccountNumber"].AsString(),
            Description = reader["Description"].AsString(),
            AmountOc = reader["AmountOc"].AsDecimal(),
            AmountVat = reader["AmountVat"].AsDecimal(),
            TaxRate = reader["TaxRate"].AsDecimal(),
            AccountNumberVat = reader["AccountNumberVat"].AsString(),
            RefId = reader["RefID"].AsLong(),
            Quantity = reader["Quantity"].AsInt(),
            Price = reader["Price"].AsDecimal(),
              VoucherTypeId = reader["VoucherTypeID"].AsIntForNull(),
          };

        public List<ItemTransactionDetail> GetItemTransactionDetailsByItemTransaction(long refId)
        {
            const string sql = @"uspGet_ItemTransactionDetail_ByMaster";
            object[] parms = { "@RefID", refId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


       public ItemTransactionDetail GetItemTransactionDetail(int itemTransactionDetailId)
        {
            const string sql = @"uspGet_ItemTransactionDetail_ByID";
            object[] parms = { "@EmployID", itemTransactionDetailId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }

       /// <summary>
       /// Inserts the itemTransactionDetail.
       /// </summary>
       /// <param name="itemTransactionDetail">The itemTransactionDetail.</param>
       /// <returns></returns>
       public string InsertItemTransactionDetail(ItemTransactionDetail itemTransactionDetail)
       {
           const string sql = "uspInsert_ItemTransactionDetail";
           return Db.Insert(sql, CommandType.StoredProcedure, Take(itemTransactionDetail));
       }

       /// <summary>
       /// Updates the itemTransactionDetail.
       /// </summary>
       /// <param name="itemTransactionDetail">The itemTransactionDetail.</param>
       /// <returns></returns>
       public string UpdateItemTransactionDetail(ItemTransactionDetail itemTransactionDetail)
       {
           const string sql = "uspUpdate_ItemTransactionDetail";
           return Db.Update(sql, CommandType.StoredProcedure, Take(itemTransactionDetail));
       }

       /// <summary>
       /// Deletes the itemTransactionDetail.
       /// </summary>
       /// <param name="itemTransactionDetail">The itemTransactionDetail.</param>
       /// <returns></returns>
       public string DeleteItemTransactionDetail(ItemTransactionDetail itemTransactionDetail)
       {
           const string sql = @"uspDelete_ItemTransactionDetail";
           object[] parms = { "@ItemTransactionDetailID", itemTransactionDetail.ItemTransactionDetailId };
           return Db.Delete(sql, CommandType.StoredProcedure, parms);
       }



       /// <summary>
       /// Gets the sequence.
       /// </summary>
       /// <returns></returns>
       public long GetSequence()
       {
           return Db.GetCount("SELECT NEXT VALUE FOR [dbo].[SEQ_ItemTransactionDetail_ItemTransactionDetailID]", null);
       }
    }
}
